/**
 * @filename:StkOtherInController Feb 5, 2025
 * @project Wonder Vue Admin Api  1.1.0
 * Copyright(c) 2020 Wonder Co. Ltd.
 * All right reserved.
 */
package com.wonder.app.controller;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Date;
import java.util.Map;

import com.wonder.admin.domain.SysAdmin;
import com.wonder.admin.service.SysAdminService;
import com.wonder.app.domain.SfcOptrpt;
import com.wonder.app.domain.SfcOptrptPackage;
import com.wonder.app.domain.StkOtherIn;
import com.wonder.app.request.SfcOperpRequest;
import com.wonder.app.request.SfcOptrptPackageRequest;
import com.wonder.app.request.SfcOptrptRequest;
import com.wonder.app.service.PrdSuffixService;
import com.wonder.app.service.SfcOptrptPackageService;
import com.wonder.app.service.SfcOptrptService;
import com.wonder.app.service.StkOtherInService;
import com.wonder.app.request.StkOtherInRequest;
import com.wonder.app.service.impl.SfcOptrptPackageServiceImpl;
import com.wonder.framework.authorization.CheckAdminToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.wonder.common.common.AjaxMap;
import com.wonder.common.common.AjaxResult;
import com.wonder.common.common.CommonPage;
import com.wonder.common.utils.MessageUtils;
import com.wonder.common.utils.StringUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;

import javax.servlet.http.HttpServletRequest;
@Api(tags = "其他入库单")
@RestController
@RequestMapping("/app/SfcOptrpt")
public class SfcOptrptController {

    @Autowired
    private SfcOptrptService sfcOptrptService;

    @Autowired
    private SysAdminService adminService;

    @Autowired
    private CheckAdminToken checkAdminToken;

    @Autowired
    private PrdSuffixService prdSuffixService;

    @Autowired
    private SfcOptrptPackageService sfcOptrptPackageService;

    @ApiOperation(value = "工序汇报列表")
    @RequestMapping(value = "/index", method = RequestMethod.POST)
    public Map<String,Object> index(@RequestBody SfcOptrptRequest sfcOptrptRequest) {
        Page<SfcOptrpt> page = new Page<SfcOptrpt>(sfcOptrptRequest.getPage(), sfcOptrptRequest.getLimit());
        IPage<SfcOptrpt> list = sfcOptrptService.selectList(sfcOptrptRequest, page);

        Map<String,Object> retMap=new HashMap<>();
        retMap.put("data",CommonPage.restPage(list));


        return AjaxMap.success(retMap);
    }

    @ApiOperation(value = "包装间工序汇报检验列表")
    @RequestMapping(value = "/package_list", method = RequestMethod.POST)
    public Map<String,Object> packageList(@RequestBody SfcOptrptPackageRequest sfcOptrptPackageRequest) {
        Page<SfcOptrptPackage> page = new Page<SfcOptrptPackage>(sfcOptrptPackageRequest.getPage(), sfcOptrptPackageRequest.getLimit());
        IPage<SfcOptrptPackage> list = sfcOptrptPackageService.packageList(sfcOptrptPackageRequest, page);

        Map<String,Object> retMap=new HashMap<>();
        retMap.put("data",CommonPage.restPage(list));


        return AjaxMap.success(retMap);
    }
    /**
     * @explain 查看对象明细
     * @param   map 对象参数：id
     * @return  JsonResult
     * @author  Wonder
     */
    @ApiOperation(value = "查询sfcoptrpt明细")
    @ResponseBody
    @PostMapping(value = "/sfcoptrptdetail")

    public AjaxResult<Map<String,Object>> getSfcOptrptDetail(HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception{
        if (null == map){
            return AjaxResult.failed("system.parameterError");
        }
        Map<String,Object> data = sfcOptrptService.getSfcOptrptDetail(map);
        if (null != data){
            return AjaxResult.success(data);
        } else {
            return AjaxResult.failed();
        }
    }
    @ApiOperation(value = "工序汇报单明细(手机端)")
    @RequestMapping(value = "/get_optrpt_is_detail", method = RequestMethod.POST)
    public AjaxResult<Map<String,Object>> getOptrptisDetail(@RequestBody Map<String,Object> map) throws Exception{
        if(null == map){
            return AjaxResult.failed("system.parameterError");
        }

        Map<String,Object> data = sfcOptrptService.getOptrptisDetail(map);
        if (null != data){
            return AjaxResult.success(data);
        } else {
            return AjaxResult.failed();
        }

    }

    /**
     * @explain 修改排序开关
     * @param   entity
     * @return  Boolean
     * @author  Wonder
     */
    @PostMapping("/updateExt")
    @ApiOperation(value = "修改排序开关", notes = "作者：Wonder")
    public AjaxResult<StkOtherIn> updateExt(@RequestBody SfcOptrpt entity){
        if (null!=entity) {
            boolean rsg = sfcOptrptService.updateById(entity);
            if (rsg) {
                return AjaxResult.success("system.updateSuccess");
            }else {
                return AjaxResult.failed("system.updateFailed");
            }
        }else {
            return AjaxResult.failed("system.parameterError");
        }
    }

    /**
     * @explain 创建工序汇报单
     * @param
     * @return  Boolean
     * @author  Wonder
     */
    @PostMapping("/create_report")
    @ApiOperation(value = "创建工序汇报单", notes = "作者：Wonder")
    public AjaxResult<?> createReport(HttpServletRequest request,@RequestBody Map<String,Object> map) throws Exception{
        String token = checkAdminToken.getTokenFormRequest(request);
        SysAdmin user = adminService.getAdminByToken(token);
        Long user_id = user.getUser_id();
        String userName = user.getName();
        boolean report = sfcOptrptService.createReport(map, userName,user_id);

        if (report){
            return AjaxResult.success(report);
        } else {
            return AjaxResult.failed();
        }

    }

    /**
     * @explain 创建工序汇报单机加车间
     * @param
     * @return  Boolean
     * @author  Wonder
     */
    @PostMapping("/create_report_jijia")
    @ApiOperation(value = "创建工序汇报单机加车间", notes = "作者：Wonder")
    public AjaxResult<?> createReportJijia(HttpServletRequest request,@RequestBody Map<String,Object> map) throws Exception{
        String token = checkAdminToken.getTokenFormRequest(request);
        SysAdmin user = adminService.getAdminByToken(token);
        Long user_id = user.getUser_id();
        String userName = user.getName();
        boolean report = sfcOptrptService.createReportJijia(map, userName,user_id);

        if (report){
            return AjaxResult.success(report);
        } else {
            return AjaxResult.failed();
        }

    }

    /**
     * @explain 审核汇报单
     * @param
     * @return  Boolean
     * @author  Wonder
     */
    @PostMapping("/audit_report")
    @ApiOperation(value = "审核工序汇报单", notes = "作者：Wonder")
    public AjaxResult<?> auditReport(HttpServletRequest request,@RequestBody Map<String,Object> map) throws Exception{
        String token = checkAdminToken.getTokenFormRequest(request);
        SysAdmin user = adminService.getAdminByToken(token);
        Long user_id = user.getUser_id();
        String userName = user.getName();
        boolean report = sfcOptrptService.auditReport(map, userName,user_id);

        if (report){
            return AjaxResult.success(report);
        } else {
            return AjaxResult.failed();
        }

    }


    /**
     * @explain 根据计划单号获取工序汇报单
     * @param
     * @return  Boolean
     * @author  Wonder
     */
    @PostMapping("/get_report_by_operNo")
    @ApiOperation(value = "根据计划单号获取工序汇报单", notes = "作者：Wonder")
    public AjaxResult<?> getReportByOperNo(HttpServletRequest request,@RequestBody Map<String,Object> map) throws Exception{
        String token = checkAdminToken.getTokenFormRequest(request);
        SysAdmin user = adminService.getAdminByToken(token);
        Long user_id = user.getUser_id();
        String userName = user.getName();
        Map<String,Object> report = sfcOptrptService.getReportByOperNo(map, userName);

        if (null != report){
            return AjaxResult.success(report);
        } else {
            return AjaxResult.failed();
        }

    }


    /**
     * @explain 保存包装间的汇报信息
     * @param
     * @return  Boolean
     * @author  Wonder
     */
    @PostMapping("/create_report_package")
    @ApiOperation(value = "保存包装间的汇报信息", notes = "作者：Wonder")
    public AjaxResult<?> createReportPackage(HttpServletRequest request,@RequestBody Map<String,Object> map) throws Exception{
        String token = checkAdminToken.getTokenFormRequest(request);
        SysAdmin user = adminService.getAdminByToken(token);
        Long user_id = user.getUser_id();
        String userName = user.getName();
        Boolean report = sfcOptrptService.createReportPackage(map, userName);

        if (report){
            return AjaxResult.success(report);
        } else {
            return AjaxResult.failed();
        }

    }

    /**
     * @explain 审核包装间的汇报信息
     * @param
     * @return  Boolean
     * @author  Wonder
     */
//    @PostMapping("/audit_report_package")
//    @ApiOperation(value = "审核包装间的汇报信息", notes = "作者：Wonder")
//    public AjaxResult<?> auditReportPackage(HttpServletRequest request,@RequestBody Map<String,Object> map) throws Exception{
//        String token = checkAdminToken.getTokenFormRequest(request);
//        SysAdmin user = adminService.getAdminByToken(token);
//        Long user_id = user.getUser_id();
//        String userName = user.getName();
//        Boolean report = sfcOptrptPackageService.auditReportPackage(map, userName,user_id);
//
//        if (report){
//            return AjaxResult.success(report);
//        } else {
//            return AjaxResult.failed();
//        }
//
//    }


    /**
     * @explain 根据托号查询汇报详情
     * @param
     * @return  Boolean
     * @author  Wonder
     */
    @PostMapping("/get_optrpt_by_tray")
    @ApiOperation(value = "创建工序汇报单", notes = "作者：Wonder")
    public AjaxResult<?> getOptrptByTray(HttpServletRequest request,@RequestBody Map<String,Object> map) throws Exception{
        String token = checkAdminToken.getTokenFormRequest(request);
        SysAdmin user = adminService.getAdminByToken(token);
        Long user_id = user.getUser_id();
        String userName = user.getName();
        Map<String,Object> data = sfcOptrptService.getOptrptByTray(map, userName,user_id);
        return AjaxResult.success(data);

    }

    /**
     * @explain 修改工序汇报详情
     * @param
     * @return  Boolean
     * @author  Wonder
     */
    @PostMapping("/update_optrpt_detail")
    @ApiOperation(value = "修改工序汇报详情", notes = "作者：Wonder")
    public AjaxResult<?> updateOprptDetail(HttpServletRequest request,@RequestBody Map<String,Object> map) throws Exception{
        String token = checkAdminToken.getTokenFormRequest(request);
        SysAdmin user = adminService.getAdminByToken(token);
        Long user_id = user.getUser_id();
        String userName = user.getName();
        Boolean report = sfcOptrptService.updateOprptDetail(map, userName,user_id);

        if (report){
            return AjaxResult.success(report);
        } else {
            return AjaxResult.failed();
        }

    }

    /**
     * @explain 查询所有后缀
     * @param   map 对象参数：id
     * @return  JsonResult
     * @author  Wonder
     */
    @PostMapping("/select_suffix")
    @ApiOperation(value = "查询所有后缀", notes = "作者：Wonder")
    public AjaxResult<?> selectSuffix(HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception{
        String token = checkAdminToken.getTokenFormRequest(request);
        SysAdmin user = adminService.getAdminByToken(token);
        String userName = user.getName();
        Long user_id = user.getUser_id();
        Map<String,Object> dataList = prdSuffixService.selectSuffix(map,user_id);
        if (null != dataList ) {
            return AjaxResult.success(dataList);
        }else {
            return AjaxResult.failed("system.parameterError");
        }

    }

    /**
     * @explain 修改包装汇报(填写外协和不良数量)
     * @param
     * @return  Boolean
     * @author  Wonder
     */
    @PostMapping("/update_package")
    @ApiOperation(value = "修改包装汇报", notes = "作者：Wonder")
    public AjaxResult<?> updatePackage(HttpServletRequest request,@RequestBody Map<String,Object> map) throws Exception{
        String token = checkAdminToken.getTokenFormRequest(request);
        SysAdmin user = adminService.getAdminByToken(token);
        Long user_id = user.getUser_id();
        String userName = user.getName();
        Boolean report = sfcOptrptPackageService.updatePackage(map, userName,user_id);

        if (report){
            return AjaxResult.success(report);
        } else {
            return AjaxResult.failed();
        }

    }
}
